package JPetStore.persistence;

import java.sql.*;

public class DBUtil {
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/mypetstore";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        Connection connection = null;
        Class.forName(DRIVER);
        connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        return connection;
    }

    public static void closeConnection(Connection connection) throws SQLException {
        if(connection != null){
            connection.close();
        }
    }

    public static void closeStatement(Statement statement) throws SQLException {
        if(statement != null){
            statement.close();
        }
    }

    public static void closeResultSet(ResultSet resultSet) throws SQLException {
        if(resultSet != null){
            resultSet.close();
        }
    }
    public static void closeStatement(Statement statement, ResultSet resultSet) throws SQLException {
        if(statement != null){
            statement.close();
        }
    }
    public static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
        if(connection != null){
            closeConnection(connection);
        }
    }

    public static void closePreparedStatement(PreparedStatement preparedStatement) throws SQLException{
        if(preparedStatement != null){
            preparedStatement.close();
        }
    }
}
